package com.hzw.saas.web.app.user.controller;

import javax.servlet.http.HttpServletRequest;

import com.hzw.saas.api.storage.IResourceFileService;
import com.hzw.saas.api.storage.bo.ResourceFileStorageBO;
import com.hzw.saas.api.storage.enums.StorageSpaceEnum;
import com.hzw.saas.api.user.ICustomerComplainService;
import com.hzw.saas.api.user.bo.CustomerComplainBO;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.enums.ConflictModeEnum;
import com.hzw.saas.service.storage.config.StorageConfig;
import com.hzw.saas.service.storage.util.StorageUtil;
import com.hzw.saas.web.app.user.dto.CustomerComplainDTO;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zzl
 * @since 08/25/2021
 */
@Controller
@RequiredArgsConstructor
@Slf4j
@Api(tags = "客户意见登记")
public class CustomerComplainController {

    private static final String CREATOR_ID = "customer-complain";

    private final HttpServletRequest request;

    private final StorageConfig storageConfig;
    private final IResourceFileService resourceFileService;
    private final ICustomerComplainService customerComplainService;

    @PostMapping(value = "/customer/complain", consumes = {"multipart/form-data"})
    @Transactional(rollbackFor = Exception.class)
    @SysLog(operation = "客户意见登记")
    @ApiOperation(value = "客户意见登记", notes = "客户意见登记")
    @ApiOperationSort(11)
    public ResponseEntity<Void> saveCustomerComplain(@Validated CustomerComplainDTO param) {
        // 判断请求类型
        boolean multipart = ServletUtil.isMultipart(request);
        AssertUtil.assertThrow("上传失败，请求参数异常，请使用multipart/form-data类型上传", !multipart);

        CustomerComplainBO bo = MapperUtil.nf().map(param, CustomerComplainBO.class);
        StringBuilder attachments = new StringBuilder("");
        if (ArrayUtil.isNotEmpty(param.getFiles())) {
            for (MultipartFile attachment : param.getFiles()) {
                ResourceFileStorageBO resourceFileBO = new ResourceFileStorageBO();
                resourceFileBO.setCreatorId(CREATOR_ID);
                resourceFileBO.setEditorId(CREATOR_ID);
                resourceFileBO.setCatalog(storageConfig.getPersonalSpaceCatalog());
                resourceFileBO.setSupId(StorageUtil.getProperFolderId(CREATOR_ID, StorageSpaceEnum.ATTACHMENT_SPACE.getCode()));
                resourceFileBO.setName(attachment.getOriginalFilename());
                resourceFileBO.setLength(attachment.getSize());
                resourceFileBO = resourceFileService.saveFile(resourceFileBO, attachment, ConflictModeEnum.ALLOW);
                if (StrUtil.isNotBlank(attachments)) {
                    attachments.append(",");
                }
                attachments.append(resourceFileBO.getPid());
            }
        }
        bo.setAttachments(attachments.toString());
        this.customerComplainService.save(bo);
        return ResponseEntity.ok().build();
    }

}
